package com.theme.passport.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.validator.ValidatorUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.annotation.Login;
import com.theme.passport.controller.app.utils.AppContextUtil;
import com.theme.passport.entity.UserEntity;
import com.theme.passport.entity.constant.DownloadTypeEnum;
import com.theme.passport.entity.theme.ThemeEntity;
import com.theme.passport.entity.theme.UserLikeEntity;
import com.theme.passport.entity.theme.WallpaperEntity;
import com.theme.passport.entity.theme.WidgeEntity;
import com.theme.passport.form.DownloadBrowseLikeBuyForm;
import com.theme.passport.service.theme.ThemeService;
import com.theme.passport.service.theme.UserLikeService;
import com.theme.passport.service.theme.WallpaperService;
import com.theme.passport.service.theme.WidgeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 我的点赞,我的喜欢
 *
 * @author live
 * @email 870459550@qq.com
 * @date 2020-11-23 10:57:59
 */
@RestController
@RequestMapping("app/user-like")
@Api("用户点赞和喜欢")
public class UserLikeController {

    @Autowired
    private UserLikeService userLikeService;

    @Autowired
    private WallpaperService wallpaperService;

    @Autowired
    private ThemeService themeService;

    @Autowired
    private WidgeService widgeService;

    @Login
    @GetMapping("/list")
    @ApiOperation("查询用户已点赞的接口")
    public R list(@RequestParam Map<String, Object> params){
        UserEntity currentUser = AppContextUtil.getCurrentUser();
        params.put("userId",currentUser.getId() +"");
        PageUtils page = userLikeService.queryPage(params);
        return R.ok().put("page", page);
    }

    @Login
    @PostMapping("/like")
    @ApiOperation("用户点赞的接口")
    public R like(@RequestBody DownloadBrowseLikeBuyForm form){
        //表单校验
        ValidatorUtils.validateEntity(form);

        UserLikeEntity userLike = new UserLikeEntity();
        UserEntity currentUser = AppContextUtil.getCurrentUser();
        userLike.setType(form.getType());
        userLike.setObjectId(form.getObjectId());
        userLike.setUserId(currentUser.getId());
        userLike.setCreated(new Date());

        QueryWrapper<UserLikeEntity> orderWrapper = new QueryWrapper<>();
        orderWrapper.and(wrapper -> wrapper
                .eq("user_id", currentUser.getId())
                .eq("type", userLike.getType())
                .eq("object_id", userLike.getObjectId()));
        List<UserLikeEntity> list = userLikeService.list(orderWrapper);
        //0不存在，新增点赞,1已存在，则取消点赞
        if (list != null && list.size()>0){
            UserLikeEntity userLikeEntity = list.get(0);
            List<Long> ids = new ArrayList<>();
            ids.add(userLikeEntity.getId());
            userLikeService.removeByIds(ids);
            return R.ok();
        }
        if (userLike.getType() == DownloadTypeEnum.THEME.getValue()){
            ThemeEntity theme = themeService.getById(userLike.getObjectId());
            if (theme != null){
                userLike.setObjectName(theme.getName());
                userLike.setImage(theme.getIcon());
            }
        }else if (userLike.getType() == DownloadTypeEnum.WIDGE.getValue()){
            WidgeEntity widge = widgeService.getById(userLike.getObjectId());
            if (widge != null){
                userLike.setObjectName(widge.getName());
                userLike.setImage(widge.getPreview());
            }
        }else {
            WallpaperEntity wallpaper = wallpaperService.getById(userLike.getObjectId());
            if (wallpaper != null){
                userLike.setObjectName(wallpaper.getName());
                userLike.setImage(wallpaper.getImage());
            }
        }
        userLikeService.save(userLike);
        return R.ok();
    }

    @Login
    @PostMapping("/unLike")
    @ApiOperation("用户取消点赞的接口")
    public R unLike(@RequestBody UserLikeEntity userLike){
        UserEntity currentUser = AppContextUtil.getCurrentUser();
        QueryWrapper<UserLikeEntity> orderWrapper = new QueryWrapper<>();
        orderWrapper.and(wrapper -> wrapper
                .eq("user_id", currentUser.getId())
                .eq("type", userLike.getType())
                .eq("object_id", userLike.getObjectId()));
        List<UserLikeEntity> list = userLikeService.list(orderWrapper);
        if (list != null && list.size()>0){
            UserLikeEntity userLikeEntity = list.get(0);
            List<Long> ids = new ArrayList<>();
            ids.add(userLikeEntity.getId());
            userLikeService.removeByIds(ids);
            return R.ok();
        }
        return R.ok();
    }


    @Login
    @PostMapping("/delete")
    @ApiOperation("删除记录接口 id数组")
    public R delete(@RequestBody Long[] ids){
        userLikeService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
